<template>
	<view class="addshop">
		<view class="item proname">
			<view class="title">商品名称:</view>
			<input placeholder="输入商品名称" v-model="proname" disabled="" class="inputitem"></input>
		</view>
		<view class="item barcode">
			<view class="title">条形码:</view>
			<view class="content">
				<input placeholder="输入条形码" v-model="barcode"></input>
				<image mode="aspectFit" src="../../static/saoyisao-2.png" class="sao" v-on:click="onScan()" v-if="isWxConfig==true"></image>
			</view>
		</view>
		<view class="item cost">
			<view class="title">进价:</view>
			<input type="digit" placeholder="输入进价" v-model="cost" disabled="" class="inputitem"></input>
		</view>
		<view class="item price">
			<view class="title">售价:</view>
			<input type="digit" placeholder="输入售价" v-model="price" disabled="" class="inputitem"></input>
		</view>
		<view class="item unit_name">
			<view class="title">单位:</view>
			<input type="digit" placeholder="输入售价" v-model="unit_name" disabled="" class="inputitem"></input>
		</view>
		<view class="item price">
			<view class="title">数量:</view>
			<input type="digit" placeholder="输入售价" v-model="nums" class="inputitem"></input>
		</view>
		<view class="scanWrapper">
			<view class="scanA" v-if="isWxConfig==false">无法扫描</view>
			<view class="scanB" v-on:click="onScan" v-if="isWxConfig==true">扫描商品</view>
			<view class="scanB" v-on:click="onPut">入库库存</view>
		</view>
	</view>
</template>

<script>
	import http from "../../common/http.js"
	import common from "../../common/common.js"
	var app = getApp()
	export default {
		data() {
			return {
				storeid:0,
				isWxConfig:false,
				proname:'',
				barcode:'',
				cost:'',
				price:'',
				unit_name:'',
				nums:0,
				id:0,
				isedit:false
			}
		},
		onLoad(options) {
			this.storeid = options.storeid
			console.log(options)
			console.log(app.globalData.a)
			var self = this
			
			common.initWxConfigIfNeed(()=>{
				self.isWxConfig = true
			})
		},
		methods: {
			onScan() {
				// this.onScanFinished("6828804011173")
				// return
				var self = this
				common.scanQrCode((barcode)=>{
					//self.barcode = barcode
					
					http.get("/store/addstock",{"storeid":self.storeid,"barcode":barcode},function(res){
						console.log(res)
						if (res.IsBig){
							self.nums=res.Exchange
						}else{
							self.nums=1
						}
						self.proname=res.ProName
						self.barcode=res.BarCode
						self.cost=(res.Cost/100).toFixed(2)
						self.price=(res.Price/100).toFixed(2)
						self.unit_name=res.UnitName
						self.id=res.Id
					})
				})
			},
			onScanFinished(text) {
				
			},
			
			onPut() {
				var self=this
				var data = {
					"id":self.id,
					"nums":self.nums,
				}
				http.post("/store/addstock", data, (r)=> {
					uni.showToast({
						title:"入库完成"
					})
					self.reset()
				})
			},
			reset() {
				this.proname = ""
				this.barcode = ""
				this.cost = ""
				this.price = ""
				this.unit_name = ""
				this.nums=0
				this.id=0
			},
			onUnit() {
				var self = this
				var arr = ["个", "只", "瓶", "件"]
				uni.showActionSheet({
					itemList:arr,
					success(res){
						self.unit_name = arr[res.tapIndex]
					}
				})
			}
		}
	}
</script>

<style>

	.scanWrapper {
		margin-top: 10px;
		width: 100%;
		display: flex;
		justify-content: space-around;
	}
	.scanA {
		background-color: #DEDEDE;
		color: white;
		text-align: center;
		height: 44px;
		width: 40%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.scanB {
		background-color: #007AFF;
		color: white;
		text-align: center;
		height: 44px;
		width: 40%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.upload {
		background-color: #007AFF;
		color: white;
		text-align: center;
		height: 50px;
		width: 80%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.item {
		padding: 10px 15px;
		position: relative;
	}
	.item .title { 
		font-size: 18px;
		height: 35px;
	}
	.inputitem {
		background-color: #f3f3f2;
		height: 44px;
		padding: 0 10px;
		border-radius: 10px;
		line-height: 44px;
		color: #666666;
		box-sizing: border-box;
	}
	.content {
		height: 44px;
		line-height: 44px;
		width: 100%;
		position: relative;
	}
	.content input {
		background-color: #f3f3f2;
		height: 44px;
		padding: 0 10px;
		border-radius: 10px;
		line-height: 44px;
		color: #666666;
	}
	.sao {
		width: 25px;
		height: 25px;
		position: absolute;
		top: 10px;
		right: 10px;
	}
</style>
